
<#include "/base/common/head.html"/>


 <div class="easyui-panel updatePage multiple" data-options="fit:true,border:false">
    <form id="ff" class="easyui-form" method="post">
        <table class="table1">
           
            <tr>
           		<th><span>车辆</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-combobox" type="text" name="car.id" id="car.id" value="<#if bo.car??>${bo.car.id!''}</#if>"  addClear tabindex="1"
               			data-options="required:true,fit: true,valueField:'id',textField:'name',url:'${basePath}/car/car/car/findAll',editable:false,
               				onSelect:function(record){
               					//选择车辆时，将 司机 赋值为车辆的默认司机
               					if(record.driver != null)
               					{
               						$('#driver').combobox('setValue', record.driver);
               					}
               					if(record.endNum != null)
               					{
               						$('#beginNum').numberbox('setValue', record.endNum);
               						$('#endNum').numberbox('setValue', record.endNum);
               					}
               				}"></input></div>
           		</td>
           		<th><span>出发时间</span></th>
               	<td >
               		<div class="wrap_input"><input class="easyui-datetimespinner" type="text" name="beginTime" id="beginTime" tabindex="9"
               		 value="<#if bo.beginTime??>${bo.beginTime?string('yyyy-MM-dd HH:mm')}</#if>"  
               			data-options="required:true,fit: true,showSeconds:false, onChange:function(){beginTimeChange();}"></input></div>
           		</td>
           		<TD  style="width:100px"></TD>
            </tr>
            <tr>
           		<th><span>司机</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-combobox" type="text" name="driver" id="driver" value="${bo.driver!''}"  addClear tabindex="2"
               			data-options="required:true,fit: true,valueField:'name',textField:'name',url:'${basePath}/base/basis/enumDetail/findByKey?key=driver'"></input></div>
           		</td>
           		<th><span>归队时间</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-datetimespinner" type="text" name="endTime" id="endTime"  tabindex="10"
               		 value="<#if bo.endTime??>${bo.endTime?string('yyyy-MM-dd HH:mm')}<#else><#if bo.beginTime??>${bo.beginTime?string('yyyy-MM-dd HH:mm')}</#if></#if>" 
               			data-options="required:true,fit: true,showSeconds:false,validType:'endTimeValidator', onChange:function(){useTime();}"></input></div>
           		</td>
            </tr>
            <tr>
           		<th><span>用车部门</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-combobox" type="text" name="organ.id" id="organ.id" value="${bo.organ.id!''}"  addClear tabindex="3"
               			data-options="required:true,fit: true,valueField:'id',textField:'name',url:'${basePath}/car/outbill/useOrgan/findAll',editable:false,
               			onSelect:function(record){
               					//选择部门时，用部门的第一责任人、第二责任人填充审批人的下拉框，同时将审批人默认为 第一责任人
               					var dataArr = [];
               					if(record.str1 != null)
               					{	
               						dataArr.push({id:record.str1,name:record.str1});
               					}
               					if(record.str2 != null)
               					{	
               						dataArr.push({id:record.str2,name:record.str2});
               					}
               					
               					$('#approver').combobox('loadData', dataArr);
               					if(record.str1 != null)
               					{	
               						$('#approver').combobox('setValue', record.str1);
               					}
               					
               			}"></input></div>
           		</td>
           		<th><span>总时长（小时）</span></th>
               	<td>
               		<span class="showFeeFrom" id="useTimeSpan">${bo.useTime!''}</span>
               		<input type="hidden" name="useTime" id="useTime" value="${bo.useTime!''}" >
           		</td>
            </tr>
            <tr>
           		<th><span>审批人</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-combobox" type="text" name="approver" id="approver" value="${bo.approver!''}"  addClear tabindex="4"
               			data-options="required:true,fit: true,valueField:'id',textField:'name'"></input></div>
           		</td>
           		<th><span>等待时长（小时）</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-numberbox" type="text" name="waitTime" id="waitTime" value="${bo.waitTime!''}"  addClear tabindex="11"
               			data-options="fit: true,precision:1, onChange:function(){allFee();}, validType:'waitTimeValidator'"></input></div>
           		</td>
            </tr>
            <tr>
           		<th><span>用车人</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-textbox" type="text" name="user" id="user" value="${bo.user!''}"  addClear tabindex="5"
               		data-options="required:true,fit: true"></input></div>
           		</td>
           		<th><span>出发里程</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-numberbox" type="text" name="beginNum" id="beginNum" value="<#if bo.beginNum??>${bo.beginNum?c}</#if>"  addClear tabindex="12"
               			data-options="required:true,fit: true, onChange:function(){allFee();}"></input></div>
           		</td>
            </tr>
            <tr>
           		<th><span>目的地</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-textbox" type="text" name="destination" id="destination" value="${bo.destination!''}"  addClear tabindex="6"
               		data-options="required:true,fit: true"></input></div>
           		</td>
           		<th><span>归队里程</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-numberbox" type="text" name="endNum" id="endNum" value="<#if bo.endNum??>${bo.endNum?c}</#if>"  addClear tabindex="13"
               			data-options="required:true,fit: true,validType:'endNumValidator', onChange:function(){allFee();}"></input></div>
           		</td>
            </tr>
            <tr>
           		<th><span>车辆状况</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-combobox" type="text" name="carStatus" id="carStatus" value="${bo.carStatus!''}" addClear tabindex="7" 
               			data-options="fit: true,valueField:'name',textField:'name',url:'${basePath}/base/basis/enumDetail/findByKey?key=carStatus'"></input></div>
           		</td>
           		<th><span>加油金额</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-numberbox" type="text" name="fuelNum" id="fuelNum" value="<#if bo.fuelNum??>${bo.fuelNum?c}</#if>" addClear tabindex="14" 
               			data-options="fit: true,precision:1"></input></div>
           		</td>
            </tr>
            
            
            <tr>
           		<th><span>备注</span></th>
               	<td colspan="1">
               		<div class="wrap_input"><input class="easyui-textbox" type="text" name="remark" id="remark" value="${bo.remark!''}" addClear tabindex="8" data-options="fit: true" ></input></div>
               		
           		</td>
           		<th><span>过 / 停费</span></th>
               	<td>
               		<div class="wrap_input"><input class="easyui-numberbox" type="text" name="otherFee" id="otherFee" value="<#if bo.otherFee??>${bo.otherFee?c}</#if>"  addClear tabindex="15"
               			data-options="fit: true,precision:1"></input></div>
           		</td>
            </tr>
            
            <tr>
           		<th><span>总费用</span></th>
               	<td colspan="3">
               		<span class="showFeeFrom" id="allFeeSpan"><#if bo.allFee??>${bo.allFee?c}</#if></span>
               		<input type="hidden" name="allFee" id="allFee" value="<#if bo.allFee??>${bo.allFee?c}</#if>" />
               		<input type="hidden" name="useNum" id="useNum" value="<#if bo.useNum??>${bo.useNum?c}</#if>" />
           		</td>
            </tr>
          
        </table>
    </form>
</div>

<style type="text/css">
.showFeeFrom{font-family: Arial, Helvetica, sans-serif; line-height: 22px; color: #2f8ec7;margin-left:10px;font-size:16px;}
.showAllFee{color: #ff0000;}
</style>

<script>
top.window.subPage.save = save;
function save() {
	if($("#ff").form('validate')) {
		$.post("update", getParam(), function(data) {
			
			if("01" == data.code){//成功
				top.window.closeWindow(); //关闭窗口
				top.window.subPage.loadCurrDatagrid(); //回调方法
				showMsg(data.msg); //显示消息
				
			}else if("04" == data.code){//失败
				showErrorMsg(data.msg); //显示错误消息
			}
			
		});
	}
}
function getParam() {
	var param = {
	     
	    "id": ${bo.id?c}, 
	    "obj.car.id": getInputValue("car.id"), 
		"obj.driver": getInputValue("driver"), 
		"obj.organ.id": getInputValue("organ.id"), 
		"obj.user": getInputValue("user"), 
		"obj.approver": getInputValue("approver"), 
		"obj.destination": getInputValue("destination"), 
		"obj.carStatus": getInputValue("carStatus"), 
		"obj.beginTime": getInputValue("beginTime"), 
		"obj.endTime": getInputValue("endTime"), 
		"obj.useTime": getInputValue("useTime"), 
		"obj.waitTime": getInputValue("waitTime"), 
		"obj.beginNum": getInputValue("beginNum"), 
		"obj.endNum": getInputValue("endNum"), 
		"obj.fuelNum": getInputValue("fuelNum"), 
		"obj.otherFee": getInputValue("otherFee"), 
		"obj.remark": getInputValue("remark"), 
		"obj.allFee": getInputValue("allFee"), 
		"obj.useNum": getInputValue("useNum")
	}
	for(key in param) {
		if(!param[key]) {
			delete param[key];
		}
	}
	return param;
}

//计算总费用
function allFee()
{
	var startingPrice = ${rate.startingPrice!'0'}; //起步价
	var unitPrice = ${rate.unitPrice!'0'}; //每公里单价
	var timePrice = ${rate.timePrice!'0'}; //每小时单价
	
	var beginNum = $("#beginNum").val(); //出发里程
	var endNum = $("#endNum").val(); //归队里程
	var waitTime = $("#waitTime").val(); //等待时长（小时）
	
	if(beginNum == "") { beginNum = "0"; }
	if(endNum == "") { endNum = "0"; }
	if(waitTime == "") { waitTime = "0"; }
	
	var useNum = parseFloat(endNum)-parseFloat(beginNum);
	
	var allFee =  parseFloat(startingPrice)	 //起步价
	+ parseFloat(unitPrice)*useNum  //公里计价
	+ parseFloat(timePrice)*parseFloat(waitTime);  //等待计价
	
	
	var allFeeStr = allFee.toFixed(2);

	var str = startingPrice+" + "+unitPrice+" * ( "+endNum+" - "+beginNum+" )";
	if(parseFloat(waitTime) != 0)
	{
		str = str + " + "+timePrice+" * "+waitTime;
	}
	
	str = str + " = <span class='showAllFee'>" + allFeeStr +"</span> 元";
	
	$("#allFeeSpan").html(str);
	$("#allFee").val(allFeeStr);
	$("#useNum").val(useNum);
}

//出发时间改变时调用
function beginTimeChange()
{
	//如果归队时间 小于 出发时间，则将归队时间改为出发时间一样的值，主要是方便录入
	var beginTimeStr = $("#beginTime").val();
	var useTimeNum = (stringToDate($("#endTime").val()).getTime() - stringToDate($("#beginTime").val()).getTime() ) / (1*60*60*1000)
	
	if(useTimeNum < 0)
	{
		$("#endTime").datetimespinner('setValue', beginTimeStr);
	}
	//计算时长 
	useTime();
}

//计算总时长
function useTime()
{
	var useTime = (stringToDate($("#endTime").val()).getTime() - stringToDate($("#beginTime").val()).getTime() ) / (1*60*60*1000)
	var useTimeStr = useTime.toFixed(1);
	
	$("#useTimeSpan").html(useTimeStr + " 小时");
	$("#useTime").val(useTimeStr);
}

//字符串转日期
function stringToDate(value){
  return new Date(Date.parse(value.replace(/-/g, "/")));
}

//自定义校验
$.extend($.fn.validatebox.defaults.rules, { 
	
	endNumValidator:{//验证归队里程
      validator: function(value){    
          return parseInt(value) > parseInt($("#beginNum").val());    
      },    
      message: '必须大于 出发里程'   
  },   

	endTimeValidator:{//验证归队时间
	    validator: function(value){    
	        return stringToDate(value) > stringToDate($("#beginTime").val());    
	    },    
	    message: '必须大于 出发时间'   
	},

	waitTimeValidator:{//验证归队时间
	    validator: function(value){
	    	
	        return stringToDate($("#endTime").val()).getTime() - stringToDate($("#beginTime").val()).getTime() > parseFloat(value)*60*60*1000 ;    
	    },    
	    message: '必须小于 总时长（ 归队时间 - 出发时间 ）'   
	} 
	    
});  

$(function(){
	/*
		为 EasyUI常用组件添加'清除'按钮及功能，共计6类，具体实现在common.js中定义，用法如下：
		（1）addClear4TextBox("#name",nameChangeDo);		//文本框,同时传入了回调函数
		（2）addClear4Combobox("#state\\.id");			//下拉列表框
		（3）addClear4Combogrid("#type\\.id");			//数据表格下拉框
		（4）addClear4Numberbox("#intNum2");				//数值输入框
		（5）addClear4Datebox("#theDate2");				//日期选择框
		（6）addClear4Datetimebox("#theTime2");			//日期选择框
		注：实现使用了 onChange 事件，如果需要使用该事件执行某些操作，可传入自定义函数，会自动回调 。
		加注：可使用autoAddClear()函数，为各组件自动填加‘清除’功能（组件需要增加 addClear属性 ）。
	*/
	autoAddClear(); //为各组件自动填加‘清除’功能（组件需要增加 addClear属性 ）
	addClear4Numberbox("#waitTime",allFee);
	addClear4Numberbox("#beginNum",allFee);
	addClear4Numberbox("#endNum",allFee);
		
	addChangeFocusOpe(); //页面增加焦点切换操作
	$("#car\\.id").next("span").find("input").focus();  //第一个元素设置焦点
		
});


</script>
<#include "/base/common/foot.html"/>
